package com.wyx.producer;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wyx.mapper.SmokeMapper;
import com.wyx.pojo.Pic;
import com.wyx.pojo.Smoke;
import com.wyx.pojo.SmokeInfoLog;
import com.wyx.service.PicService;
import com.wyx.service.SmokeInfoLogService;
import com.wyx.service.SmokeService;
import com.wyx.utils.commonUtil.CUtil;
import org.apache.poi.ss.util.CellUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

// 对实际开发维护的部分都在这里
@SpringBootTest
public class Producer {
    @Autowired
    private SmokeService smokeService;
    @Autowired
    private SmokeInfoLogService smokeInfoLogService;
    @Autowired
    private PicService picService;
    @Autowired
    private SmokeMapper smokeMapper;

    // 给数据添加   商品唯一索引id
    @Test
    public void p1() {
        List<Smoke> list = smokeService.list();
        List<SmokeInfoLog> list1 = smokeInfoLogService.list();
        Map<String, SmokeInfoLog> collect = list1.stream().collect(Collectors.toMap(SmokeInfoLog::getProductName, smokeInfoLog -> smokeInfoLog));

        ArrayList<SmokeInfoLog> smokeInfoLogs = new ArrayList<>();

        for (Smoke smoke : list) {
            String uuid = CUtil.getUUID();
            String productName = smoke.getProductName();
            SmokeInfoLog smokeInfoLog = collect.get(productName);
            if (smokeInfoLog != null) {
                smoke.setItemUniqueIndexId(uuid);
                smokeInfoLog.setItemUniqueIndexId(uuid);
                smokeInfoLogs.add(smokeInfoLog);
            }
        }
        smokeService.updateBatchById(list);
        smokeInfoLogService.updateBatchById(smokeInfoLogs);
    }

    // 修改smoke和pic对应的id 为itemId
    @Test
    public void p2() {
        List<Pic> list = picService.list();
        List<Pic> picUpdateList = new ArrayList<>();
        for (Pic pic : list) {
            String objId = pic.getObjId();
            Smoke smoke = smokeMapper.selectById(objId);
            String itemUniqueIndexId = smoke.getItemUniqueIndexId();
            pic.setObjId(itemUniqueIndexId);
            picUpdateList.add(pic);
        }
        picService.updateBatchById(picUpdateList);
    }
    // productUUId里面没有值  datastatus 里面的
    @Test
    public void test3(){
        LambdaQueryWrapper<SmokeInfoLog> smokeInfoLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        smokeInfoLogLambdaQueryWrapper.eq(SmokeInfoLog::getDataStatus,'3');
        List<SmokeInfoLog> list = smokeInfoLogService.list(smokeInfoLogLambdaQueryWrapper);
        for (SmokeInfoLog smokeInfoLog : list) {
            System.out.println(smokeInfoLog);
        }
        List<Smoke> smokes = smokeMapper.selectAllLatestSmoke();
        Map<String, Smoke> collect = smokes.stream().collect(Collectors.toMap(Smoke::getItemUniqueIndexId, smoke -> smoke));

        for (SmokeInfoLog smokeInfoLog : list) {
            String itemUniqueIndexId = smokeInfoLog.getItemUniqueIndexId();
            Smoke smoke = collect.get(itemUniqueIndexId);
            String productUuid = smoke.getProductUuid();
            smokeInfoLog.setProductUuid(productUuid);
        }

        System.out.println(1111);

        boolean b = smokeInfoLogService.updateBatchById(list);

    }
}
